<template>
  <content-box>
    <div slot="body">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" prop="region">
          <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动时间" required>
          <el-col :span="11">
            <el-form-item prop="date1">
              <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item prop="date2">
              <el-time-picker type="fixed-time" placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="即时配送" prop="delivery">
          <el-switch v-model="ruleForm.delivery"></el-switch>
        </el-form-item>
        <el-form-item label="活动性质" prop="type">
          <el-checkbox-group v-model="ruleForm.type">
            <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
            <el-checkbox label="地推活动" name="type"></el-checkbox>
            <el-checkbox label="线下主题活动" name="type"></el-checkbox>
            <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="特殊资源" prop="resource">
          <el-radio-group v-model="ruleForm.resource">
            <el-radio label="线上品牌商赞助"></el-radio>
            <el-radio label="线下场地免费"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动形式" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </content-box>
</template>

<script>
  import { mapGetters } from 'vuex'
  import ContentBox from '@/views/layout/components/ContentBox'
  import { getUserList, userDel, userRoles, saveUserRoles, userDepts, saveUserDepts } from '@/api/user'
  import { getRoleList } from '@/api/role'
  import { getDepartmentList } from '@/api/department'
  import '@/styles/tree-menus.scss'

  export default {
    components: {
      ContentBox
    },
    computed: {
      ...mapGetters([
        'fullHeight',
        'pageList'
      ])
    },
    data() {
      return {
        searchKey: '',
        ruleForm: [],
        currentRow: {},
        dialogVisible: false,
        dialogDeptVisible: false,
        dialogDeptLoading: false,
        listLoading: false,
        tableData: {
          rows: [],
          pagination: {
            total: 0,
            page: 1,
            limit: 10,
            parentId: 0
          }
        },
        roleData: [],
        deptData: [],
        selectDept: [],
        selectRole: [],
        dialogTitle: '',
        dialogLoading: false,
        defaultProps: {
          children: 'children',
          label: 'name',
          id: 'id'
        },
        defaultDepts: {
          children: 'children',
          label: 'name',
          id: 'id'
        }
      }
    },
    methods: {
      tableFull(full) {
        return this.z_fullNum(this.fullHeight, full)
      },
      search(e) {
        this.load()
      },
      handleSizeChange(val) {
        this.tableData.pagination.limit = val
        this.load()
      },
      handleCurrentChange(val) {
        this.tableData.pagination.page = val
        this.load()
      },
      // 配置部门
      handleDeptConfig(index, row) {
        this.currentRow = row
        this.dialogTitle = ' - ' + row.nickname
        this.dialogDeptVisible = true
        this.dialogDeptLoading = true
        // 获取部门列表
        getDepartmentList().then(res => {
          this.deptData = []
          this.deptData.push(...res.data)
          this.dialogDeptLoading = false
        })
        // 获取所选角色
        userDepts({ id: row.id }).then(res => {
          this.selectDept = []
          this.selectDept = res.data
        })
      },
      // 配置角色
      handleRoleConfig(index, row) {
        this.currentRow = row
        this.dialogTitle = ' - ' + row.nickname
        this.dialogVisible = true
        // 获取角色列表
        getRoleList().then(res => {
          this.roleData = []
          this.roleData.push(...res.data)
          this.dialogLoading = false
        })
        // 获取所选角色
        userRoles({ id: row.id }).then(res => {
          this.selectRole = []
          this.selectRole = res.data
        })
      },
      // 保存用户部门
      submitUserDepts() {
        this.dialogDeptLoading = true
        const checkedKeys = this.$refs.deptsTree.getCheckedKeys()
        saveUserDepts({ id: this.currentRow.id, depts: checkedKeys }).then(res => {
          console.log('返回数据', res, checkedKeys)
          this.dialogDeptLoading = false
          this.dialogDeptVisible = false
        })
      },
      // 保存用户角色
      submitUserRoles() {
        this.dialogLoading = true
        const checkedKeys = this.$refs.roleTree.getCheckedKeys()
        saveUserRoles({ id: this.currentRow.id, roles: checkedKeys }).then(res => {
          console.log('返回数据', res, checkedKeys)
          this.dialogLoading = false
          this.dialogVisible = false
        })
      },
      // 修改
      handleEdit(index, row) {
        console.log(index, row, '修改')
        // this.$router.push({path: 'userAdd', query: {id: row.id}})
      },
      // 删除
      handleDelete(index, row) {
        userDel({
          id: row.id
        }).then(res => {
          // 刷新
          this.load()
        })
      },
      // 加载数据
      load() {
        this.listLoading = true
        getUserList({
          searchKey: this.searchKey,
          limit: this.tableData.pagination.limit,
          page: this.tableData.pagination.page
        }).then(res => {
          this.tableData.rows = res.data.rows
          this.tableData.pagination.total = res.data.total
          this.listLoading = false
          console.log(res)
        })
      }
    },
    created() {
      // 初始化数据
      this.load()
    }
  }
</script>
